<?php

/**
 * 自动生成 SysGroupController
 * 
 * Apps/Backend/Controller/SysGroupController.class.php
 * Zix @ 2015-06-25
 */

namespace Backend\Controller;

use Common\Service\SysGroupService;
use Common\Service\SysDictionaryService;

class SysGroupController extends BackendController {

  public function index() {
    $action = I('get.action', 'read');

    $data = $this->_init_data();
    $data['module_name'] = '系统用户组';
    $data['module_url'] = U( $this->base_path . 'index');

    $SysGroup = SysGroupService::instance();
    $data['config'] = array(
      'action' => $action,
      'action_label' => $this->action_label[$action],
      'menu_id' => $this->menu_id ,
      'module_url' => U($this->base_path . 'index'),
      'read_url'   => U($this->base_path . 'read' ),
      'create_url' => U($this->base_path . 'create'),
      'update_url' => U($this->base_path . 'update'),
      'delete_url' => U($this->base_path . 'delete'),
      'sys_type' => self::SYS_TYPE ,
      'get_permission_url' => U($this->base_path . 'get_permission'),
      'update_permission_url' => U($this->base_path . 'update_permission'),
      'page' => 1,
      'page_size' => C('PAGE_SIZE'),
      'row' => $SysGroup->get_default_row() 
    );

    $data['param'] = array(
      'status' => array( 0 => '禁用' , 1 => '启用'),
      'group_data' => $SysGroup->get_by_cond(array(
        'sys_type' => self::SYS_TYPE ,
        'status' => 1 ,
      )) ,
    );

    $this->display_with_layout($data, $this->controller .':index');
  }
  
  function read() {
    $post = I('post.');

    $config = array(
      'sys_type' => self::SYS_TYPE ,
      'rank' => $this->auth['rank'] ,
    );

    $SysGroup = SysGroupService::instance();
    $data['rows'] = $SysGroup->get_by_cond($config);
    $this->ajaxReturn($data);
  }

  function create() {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }
    
    $SysGroup = SysGroupService::instance();
    $ret = $SysGroup->create($post);
    $this->ajaxReturn($ret);
  }

  function update($id = 0) {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }

    if ( $id == $post['pid'] ) {
      $this->ajaxReturn(ajax_arr('上级选择错误'));
    }
    
    $SysGroup = SysGroupService::instance();
    $ret = $SysGroup->update($id, $post);
    $this->ajaxReturn($ret);
  }

  function delete() {
    $ids = I('post.ids');
    $SysGroup = SysGroupService::instance();
    $ret = $SysGroup->delete($ids); 
    $this->ajaxReturn($ret);
  }    
}
